
<template>
  <div class="main">
    <PureTableBar title="价款支付列表" class="list_hei">
      <template #buttons>
        <el-button type="primary" @click="DialogVisible = true">新增价款支付 </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" adaptive size="small"
          :data="dataList" max-height="600" :columns="columns" :checkList="checkList" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }">
        </pure-table>
      </template>
    </PureTableBar>
  </div>

  <ReDialog :width="800" :height="400" :models="DialogVisible" @Closes="cancelTransfer()">
    <template #header>
      <h2 style="text-align: center;">转账窗口</h2>
    </template>
    <template #main>
      <div class="fz-14">
        <div>
          资产证券化项目：<el-select placeholder="请选择资产证券化项目" v-model="filterSelectId" style="width:40%">
            <el-option v-for="item in filterDataList" :key="item.assetTransferId" :label="item.assetSecuritizationName"
              :value="item.assetTransferId" @click="filterProject(item)" />
          </el-select>
        </div>
        <div style="margin-top:20px">
          <p>根据资产证券化项目承销与服务协议约定：</p>
          <div style="margin-top:20px">
            <p type="primary" style="padding: 0 45px;">发行资产支持证券总量是{{ formData.count }}
              份，票面金额100元，发行总规模是{{ formData.accountMoney
              }}元，发行完毕后，发行收入一共{{ formData.accountMoney }}元，现在需要向金融公司转入发行收入来支付基础资产价款，共计{{ formData.accountMoney }}元。</p>
          </div>
          <p style=" margin-top: 20px;"></p>
        </div>
      </div>
      <el-form label-width="120px">
        <el-form-item label="转账方：">
          {{ formData.spvName }}
        </el-form-item>
        <el-form-item label="收款方：">
          金融公司
        </el-form-item>
        <el-form-item label="转账金额：">
          {{ formData.accountMoney }}元
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="saveTransfer()"> 确定 </el-button>
        <el-button @click="cancelTransfer()"> 取消 </el-button>
      </span>
    </template>
  </ReDialog>
</template>
<script setup lang='ts'>
import { emitter } from "@/utils/mitt";
import { PureTableBar } from "@/components/RePureTableBar";
import {
  CirclePlusFilled
} from "@element-plus/icons-vue"; import {
  GetSPVName,
  GetPassAuditProjectName,
  GetExcessCollateral,
  FinancialTransactionsUsersGetByUsername,
  TransactionsOredrsCreate,
  UpdatePaymentStatusByAssetPoolId,
  IsTransferService,
  SecuritiesServiceChargCreate,
  GetSecuritiesHaveBeenPaid
} from "@/api/stu/SupplyChainFinance";
import { ElMessage } from "element-plus";
import qs from "qs";
import { ref, reactive, onMounted, watch } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const formData = reactive({
  count: '',
  accountMoney: '',
  spvName: '',
})
const columns: TableColumnList = [
  {
    label: "序号",
    type: "index",
    fixed: "left",
    minWidth: 50
  },
  {
    label: "项目名称",
    prop: "securityCreditRatingName",
  },
  {
    label: "转账方",
    prop: "fromName",
  },
  {
    label: "收款方",
    prop: "toAccount",
  },
  {
    label: "金额（元）",
    prop: "banlance",
  },

];;
const payData = reactive({
  fromTransactionsUserId: '',
  toTransactionsUserId: '',
  menuId: '',
  amount: '',
  description: ''
})


const dataList = ref([])
const filterDataList = ref([])
const filterSelectId = ref()
const DialogVisible = ref(false)
const selectItem = ref()//选中的项目

async function saveTransfer() {
  if (!filterSelectId.value) {
    ElMessage.warning('请选择资产证券化项目')
    return
  }
  const params = qs.stringify({
    fromName: 'spv',
    toAccount: '金融公司',
    type: 1,
    securityCreditRatingId: selectItem.value.securityCreditRatingId,
  })
  const data = {
    taskId: route.query.taskId,
    planId: route.query.planId,
  }
  let res = await IsTransferService(params, data)
  if (!res.data) {
    const data = qs.stringify({
      userName: 'SPV',
      taskId: route.query.taskId,
      planId: route.query.planId,
    })
    let res = await FinancialTransactionsUsersGetByUsername(data)
    const fromTransactionsUserId = res.data.transactionsUserId
    const data1 = qs.stringify({
      userName: '金融公司',
      taskId: route.query.taskId,
      planId: route.query.planId,
    })
    let res1 = await FinancialTransactionsUsersGetByUsername(data1)
    const toTransactionsUserId = res1.data.transactionsUserId
    const data2 = {
      fromTransactionsUserId: fromTransactionsUserId,
      toTransactionsUserId: toTransactionsUserId,
      menuId: '',
      amount: formData.accountMoney,
      description: 'spv价款支付',
      taskId: route.query.taskId,
      planId: route.query.planId,
      PurchaseOrderId: selectItem.value.assetPoolId,
    }
    let res2 = await TransactionsOredrsCreate(data2)
    if (res2.success) {
      ElMessage.success(res2.msg)
      emitter.emit('refreshMoney')
      const data = {
        securitiesServiceChargeId: "",
        taskId: route.query.taskId,
        planId: route.query.planId,
        securityCreditRatingId: selectItem.value.securityCreditRatingId,
        fromName: "spv",
        toAccount: "金融公司",
        banlance: formData.accountMoney,
        type: 1
      }
      let res4 = await SecuritiesServiceChargCreate(data)
      getList()
      let res3 = await UpdatePaymentStatusByAssetPoolId(qs.stringify({ id: selectItem.value.assetPoolId }))
      if (res3.success) {
      } else {
        ElMessage.error(res3.msg)
      }
      cancelTransfer()
    } else {
      ElMessage.error(res2.msg)
    }
  } else {
    ElMessage.warning('此项目已支付款项！')
  }
}

function cancelTransfer() {
  formData.count = ''
  formData.accountMoney = ''
  formData.spvName = ''
  filterSelectId.value = ''
  DialogVisible.value = false
}
async function filterProject(item) {
  selectItem.value = item
  const data = qs.stringify({
    assetTransferId: filterSelectId.value
  })
  let res = await GetSPVName(data)
  if (res.success) {
    formData.spvName = res.data.spvCompanyName
  } else {
    ElMessage.warning(res.msg)
  }
  let res1 = await GetExcessCollateral(data)
  if (res1.success) {
    formData.accountMoney = res1.data
    formData.count = res1.data / 100
  } else {
    ElMessage.warning(res1.msg)
  }
}
onMounted(async () => {
  getList()
  const data = {
    taskId: route.query.taskId,
    planId: route.query.planId,
  }
  let res = await GetPassAuditProjectName(data)
  if (res.success) {
    filterDataList.value = res.data
  } else {
    ElMessage.error(res.msg)
  }
})


async function getList() {
  const params = qs.stringify({
    fromName: "spv",
    toAccount: "金融公司",
    type: 1
  })
  const data = {
    taskId: route.query.taskId,
    planId: route.query.planId,
  }
  let res = await GetSecuritiesHaveBeenPaid(params, data)
  if (res.success) {
    dataList.value = res.data
  } else {
    ElMessage.error(res.msg)
  }
}
</script>
<style scoped lang="scss">
@import url(@/style/CreditComm.scss);


:deep(.el-form-item__label) {
  font-weight: 700;
  font-size: 14px
}

.fz-14 {
  font-size: 14px;
}

.flex-direction-column {
  flex-direction: column
}
</style>
